package com.gec.it.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.it.entity.Depts;
import com.gec.it.entity.Doctors;
import com.gec.it.entity.Jobs;
import com.gec.it.service.DeptsService;
import com.gec.it.service.DoctorsService;
import com.gec.it.service.JobsService;
import com.gec.it.util.EmailUtil;
import com.gec.it.util.Md5Util;
import com.gec.it.util.SmsUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 雄哥爱吃肉
 * @since 2022-01-04
 */
@Controller
public class DoctorsController {

    @Autowired
    private DoctorsService service;
    @Autowired
    private JobsService jobsService;
    @Autowired
    private DeptsService deptsService;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private SmsUtil smsUtil;
    @RequestMapping("/login")
    public String login(){
        SecurityUtils.getSubject().logout();//注销用户
        return "login";
    }
//    @PostMapping("/login")
//    public String login(String uname, String pwd, HttpSession session, Model m){
//        QueryWrapper<Doctors> qw=new QueryWrapper<>();
//        qw.lambda().eq(Doctors::getDocName, uname).eq(Doctors::getDocPwd, Md5Util.digest(pwd));
//        Doctors u=service.getOne(qw);
//        if(u!=null){
//            session.setAttribute("user", u);
//            return "redirect:index";
//        }
//        m.addAttribute("err", "账号与密码错误asdasdas");
//        return "login";
//    }
    @GetMapping("/report")
    public String report(Model m){
        List<Doctors> list=service.list2();
        m.addAttribute("list",list);
        return "report";
    }

    @GetMapping("/index")
    public String showList(Model m){
        Doctors doc= (Doctors) SecurityUtils.getSubject().getPrincipal();

        m.addAttribute("doc",doc);
        return "index";
    }

    @RequiresPermissions("doc:add")
    @GetMapping("/add")
    public String add( Model m){
        List<Jobs> jobsList=jobsService.list2();
        List<Depts> deptsList=deptsService.list2();
        m.addAttribute("jList",jobsList);
        m.addAttribute("dList",deptsList);
        return "doc_add";
    }
    @PostMapping("/add")
    //@ResponseBody
    public String add(Doctors d, MultipartFile Photo, HttpServletRequest req) throws IOException {
        if(!Photo.isEmpty()) {
            //重命名图片
            String filename = Photo.getOriginalFilename();
            String ext=filename.substring(filename.lastIndexOf("."));//获取后缀.jpg .png
            String newFilename= UUID.randomUUID().toString()+ext;//3545-d.......png

            //保存图片
            String imgpath=req.getServletContext().getInitParameter("doc_imgs");//读配置
            String webapps=new File(req.getServletContext().getRealPath("/")).getParent();//获取tomcat的webapps目录路径
            //自动创建文件
            File dir=new File(webapps+imgpath);
            System.out.println(dir);
            if(!dir.exists()){
                dir.mkdirs();
            }
            Photo.transferTo(new File(webapps+imgpath+"/"+newFilename));
            d.setDocPhoto(newFilename);
        }
        System.out.println(d);
        d.setDocPwd(Md5Util.digest(d.getDocPwd()));
        service.save(d);
        //emailUtil.sendMail("1304055639@qq.com","账户创建成功","尊敬的"+d.getDocName()+",您的系统账号已创建，可以登录操作！欢迎加入团队。");
        //smsUtil.sendSms("19124305142","验证码：8888");
        return "redirect:/list";
    }
    @RequiresPermissions("doc:list")
    @GetMapping("/list")
    public String list(Model m){
        List<Doctors> list =service.list2();
        m.addAttribute("list",list);
        return "doc_list";
    }

    @RequiresPermissions("doc:edit")
    @GetMapping("/edit")
    public String edit(Model m,int docid){
        Doctors doc= service.getById2(docid);
        m.addAttribute("doc",doc);
        List<Doctors> docList=service.list2();
        m.addAttribute("docList",docList);
        return "doc_edit";
    }
    @PostMapping("/edit")
    public String edit(Doctors doc){
        service.updateById(doc);
        return  "redirect:/list";
    }

    @RequiresPermissions("doc:del")
    @GetMapping("/del")
    public String del(int docid){
        service.removeById(docid);
        return "redirect:/list";
    }

}
